<template>
  <div class="register-container">
    <el-card class="register-card">
      <div class="title">管理员注册</div>
      <el-form :model="registerForm" :rules="rules" ref="registerForm" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="registerForm.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="goToLogin" style="width: 100%">已有账号？返回登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'Register',
  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value !== this.registerForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      registerForm: {
        username: '',
        password: '',
        confirmPassword: '',
        name: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            console.log('准备发送注册请求...');
            // 使用完整URL，避免baseURL配置问题
            const apiUrl = 'http://localhost:5000/api/admin/register';
            console.log('请求URL:', apiUrl);
            
            const registerData = {
              username: this.registerForm.username,
              password: this.registerForm.password,
              name: this.registerForm.name
            };
            console.log('请求数据:', registerData);
            
            // 添加自定义请求配置
            const requestConfig = {
              headers: {
                'Content-Type': 'application/json',
                'Cache-Control': 'no-cache',
                'Accept': 'application/json'
              }
            };
            
            console.log('请求配置:', requestConfig);
            
            const response = await axios.post(apiUrl, registerData, requestConfig);
            console.log('收到响应:', response);
            
            if (response.data.message && response.data.message.includes('成功')) {
              this.$message.success('注册成功');
              this.$router.push('/login');
            } else {
              this.$message.error(response.data.message || '注册失败');
            }
          } catch (error) {
            console.error('注册请求错误:', error);
            if (error.response) {
              console.error('错误响应:', error.response.data);
              this.$message.error(`注册失败: ${error.response.data.message || '服务器错误'}`);
            } else if (error.request) {
              console.error('无响应:', error.request);
              this.$message.error('服务器无响应，请检查网络连接或服务器状态');
            } else {
              console.error('请求配置错误:', error.message);
              this.$message.error('注册失败：' + error.message);
            }
          }
        }
      })
    },
    goToLogin() {
      this.$router.push('/login')
    }
  }
}
</script>

<style scoped>
.register-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f3f3f3;
}

.register-card {
  width: 400px;
}

.title {
  text-align: center;
  font-size: 24px;
  margin-bottom: 30px;
  color: #333;
}
</style> 